package com.dsiab.manageService.mail;

import com.dsiab.manageService.mapper.AccountDao;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.io.File;
import java.io.IOException;
import java.util.Map;

@Service
@Slf4j
public class MailSenderBoot {
    @Autowired
    JavaMailSender javaMailSender;
    @Autowired
    AccountDao accountDao;
    // 自动注入FreeMarker配置类,用户获取模板
    @Autowired
    private Configuration configuration;

    public void sendMail(String mailTo,String content, String title){
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        Map siteInfo = accountDao.getSiteInfo();
        simpleMailMessage.setFrom((String)siteInfo.get("siteEmail"));
        simpleMailMessage.setTo(mailTo);
        simpleMailMessage.setSubject(title);
        simpleMailMessage.setText(content);
        javaMailSender.send(simpleMailMessage);
    }

    public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper;
        try {
            messageHelper = new MimeMessageHelper(message,true);
            messageHelper.setFrom(to);
            messageHelper.setTo(to);
            messageHelper.setSubject(subject);
            messageHelper.setText(content,true);
            //携带附件
            FileSystemResource file = new FileSystemResource(filePath);
            String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
            messageHelper.addAttachment(fileName,file);

            javaMailSender.send(message);
            log.info("邮件加附件发送成功！");
        } catch (MessagingException e) {
            log.error("发送失败："+e);
        }
    }

    public void sendMailByTemplate(String mailTo,String title,Map formData, String templateName) throws IOException, TemplateException, MessagingException {


        Template template = configuration.getTemplate(templateName);
        String contents = FreeMarkerTemplateUtils.processTemplateIntoString(template, formData);
        System.out.println("--->"+contents);

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        /** 设置邮件重要性级别 */
        mimeMessage.setHeader("Importance","High");
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        helper.setFrom("2381773226@qq.com");
        helper.setTo(mailTo);
        helper.setSubject(title);
        helper.setText(contents,true);
        javaMailSender.send(mimeMessage);
    }
}
